<?php
        require_once('auth.php');
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<HEAD>
<TITLE>REDCAT Error Analysis Results</TITLE>
<link href="style.css" rel="stylesheet" type="text/css" />
</HEAD>
<script type="text/javascript">
function newPopup(url) {
        popupWindow = window.open(
        url, 'popUpWindow', 'height=450,width=800,left=50,top=50,resizable=yes,scrollbars=yes,toolbar=no,menubar=no,location=no,directories=no,status=yes')
}
function newPopup2(url) {
        popupWindow = window.open(
        url,'popUpWindow','height=450,width=400,left=50,top=50,resizable=yes,scrollbars=yes,toolbar=no,menubar=no,location=no,directories=no,status=yes')
}
</script>
<BODY>
<center>
<img src="redcat_logo.jpg">
</center>
<a href="JavaScript:newPopup('account.php');" style="float:left">My Files</a>
<a href="logout.php" style="float:right">Logout</a>
</div><br />

<div>
<a href="JavaScript:newPopup2('help-menu.php');" style="float:left">Click here for help</a>
</div><br /><br />

<div>
  <form method="GET" action="/REDCAT/error.php"><input type="submit" name="rerun" id="rerun" value="Re-Calculate Error Analysis" style="float:left"></form>
  <form method="GET" action="/REDCAT/return-upload.php"><input type="submit" name="return" id="return" value="Return to Data" style="float:right"></form>
</div><br /><br >

<style type="text/css">
tr.filelisting:hover { background-color:#FFC }
</style>

<?php
//Find the current session file
$login = $_SESSION['SESS_LOGIN'];
$cwd = getcwd();
$target = "workspace/".$login;
$file = $_SESSION['SESS_CURR_FILE'];

//PERL APPS REDCAT CONVERT.PRL filename A.txt b.txt
$cmd0 = `perl /usr/local/bin/convert.prl $file $target/A.txt $target/b.txt`;

//Command to get the number of lines in the file
$cmd1 =`wc $file > $target/wc.txt`;

$wc = file($target."/wc.txt");

//Parse output of wc to get the number of lines in the file
foreach($wc as $line)
{
	$value = preg_split("/[\s]+/", $line);
	$lines = $value[1];
	break;
}

//Command to create svd.out file
chdir($target);
$cmd2 =`svd $lines 5 A.txt`;

//Command to run REDCAT file
$seed = mt_rand();
$cmd3 =`solve $lines 5 svd.out b.txt 10000 10 1 1 $seed > solve.txt`;
chdir($cwd);

//Grab and output results onto screen by line
$solve = fopen($target."/solve.txt", "r");
$l1 = fgets($solve);
echo $l1;
$data = file($file);
echo '<H5 align = "center">';
echo '<table border="1">';
echo '<tr><th align = "center">Eq.#</th><th align = "center">X1</th><th align = "center">Y1</th><th align = "center">Z1</th><th align = "center">X2</th><th align = "center">Y2</th><th align = "center">Z2</th><th align = "center">Dipol</th><th align = "center">Error</th><th align = "center">Comments</th><th align = "center">RMSD</th></tr>';
$eq = 1;
foreach($data as $line)
{
        $l1 = fgets($solve);
        $error = preg_split("/[\s]+/", $l1);
        $comment = preg_split('(\/\*)', $line);
        $value = preg_split("/[\s]+/", $comment[0]);
        echo "<tr class = \"filelisting\"><td>$eq</td><td>$value[0]</td><td>$value[1]</td><td>$value[2]</td><td>$value[3]</td><td>$value[4]</td><td>$value[5]</td><td>$value[6]</td><td>$value[8]</td><td>/*$comment[1]</td>";
        if($error[7] > $value[8])
        {
                echo "<td style=\"color: red\">$error[7]</td></tr>";
        }
        else
        {
                echo "<td style =\"color: green\">$error[7]</td></tr>";
        }
        $eq++;
}
echo '</table>';
fclose($solve);

session_write_close();
?>

</HTML>

